class_name AttributeModifier
extends Resource

enum OperationType
{
	ADD,
	SUB,
	MUL,
	DIV,
	PERCENTAGE,
	SET,
}

#region 属性
var attribute:Attribute
var type:OperationType
var value
var timer:CustomTimer
var args:Dictionary = {}
# 唯一ID，相同ID会先移除已有的，再添加新的
var unique_id:String
# 特殊属性
var tag:String
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 静态方法
static func create(attribute:Attribute,type:OperationType,value,duration:float,args:Dictionary = {}) -> AttributeModifier:
	return AttributeModifier.new(attribute,type,value,duration,args)
#endregion
#region 基类方法
#endregion
#region 公共方法
func operate(base_value):
	if attribute.base_value.property_type == CompositeProperty.PropertyType.Int or attribute.base_value.property_type == CompositeProperty.PropertyType.Float:
		match type:
			OperationType.ADD:return base_value + value
			OperationType.SUB:return base_value - value
			OperationType.MUL:return base_value * value
			OperationType.DIV:return 0 if is_zero_approx(value) else base_value / value
			#OperationType.PERCENTAGE:return base_value * value
			OperationType.SET:return value
			_:return base_value
	return base_value
#endregion
#region 私有方法
func _on_timer_finished(timer_result:CustomTimer.TimerResult):
	if timer_result == CustomTimer.TimerResult.Completed:
		attribute.remove_modifier(self)
#endregion
#region 生命周期函数
func _init(attribute:Attribute,type:OperationType = OperationType.ADD,value = null,duration:float = 0,args:Dictionary = {}) -> void:
	self.attribute = attribute
	self.type = type
	self.value = value
	self.args = args
	self.unique_id = args.get("UniqueID",null)
	self.tag = self.args.get("Tag","Default")
	if duration > 0:
		timer = GameCustomTimerSystem.create_timer(duration)
		timer.register_listener(timer.timer_finished,_on_timer_finished)
	if timer:
		timer.start_timer()
#endregion
